﻿Imports System.Runtime.InteropServices
Imports System.IO

Public Class FrmError
    Private Shared ReadOnly HWND_NOTOPMOST As New IntPtr(-2)
    Private Shared ReadOnly HWND_TOPMOST As New IntPtr(-1)
    Dim ErrorsPath As String = Application.StartupPath
    Private Const SWP_NOSIZE As Integer = &H1
    Private Const SWP_NOMOVE As Integer = &H2
    Dim StatusLastSize As Integer = Nothing
    Dim FileLastSize As Integer = Nothing
    Dim DoneOnTop As Boolean = False

    <DllImport("user32.dll", SetLastError:=True)> Private Shared Function SetWindowPos _
    (ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, _
     ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean
    End Function

    Protected Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            Const WS_EX_TOPMOST As Integer = &H8

            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or WS_EX_TOPMOST
            Return cp
        End Get
    End Property

    Public Function MakeTopMost()
        SetWindowPos(Me.Handle(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
        Return Nothing
    End Function

    Public Function MakeNormal()
        SetWindowPos(Me.Handle(), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
        Return Nothing
    End Function

    Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
        ListErrors.Text = Nothing
        Application.Exit()
    End Sub

    Private Sub FrmError_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        MakeTopMost()
    End Sub

    Private Sub BtnContinue_Click(sender As Object, e As EventArgs) Handles BtnContinue.Click

    End Sub

    Private Sub ListErrors_TextChanged(sender As Object, e As EventArgs) Handles ListErrors.TextChanged
        Try
            Dim MyStream As FileStream
            Dim MyWriter As StreamWriter
            Dim FullPath As String = ErrorsPath & "\errors.txt"
            MyStream = New FileStream((FullPath), FileMode.Append)
            MyWriter = New StreamWriter(MyStream)
            Dim MyItem As Object
            For Each MyItem In ListErrors.Lines
                MyWriter.Write(MyItem.ToString & Environment.NewLine)
            Next
            MyWriter.Close()
            MyStream.Close()
        Catch ex As Exception
            MsgBox("Couldn't save error log, Error: " & ex.Message, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Title:="Error")
        End Try
    End Sub
End Class